package com.zerowidth.db.op;

import com.zerowidth.db.DBHelper;
import com.zerowidth.db.bean.GameResultBean;
import com.zerowidth.db.utils.IOUtils;

import java.sql.ResultSet;
import java.sql.SQLException;

public class GameResultDB {

    public static GameResultBean findResult(int runCount) {

        String sql = String.format("SELECT * FROM game_result_controller WHERE start_count <= %d " +
                " AND end_count >= %d ", runCount, runCount);

        ResultSet rs = DBHelper.executeQuery(sql);

        try {
            if (rs.next()) {
                GameResultBean bean = new GameResultBean();
                bean.type = rs.getInt("type");
                bean.start_count = rs.getInt("start_count");
                bean.end_count = rs.getInt("end_count");
                bean.first_run_light_index = rs.getString("first_run_light_index");
                bean.rate = rs.getInt("rate");
                bean.luck_type = rs.getInt("luck_type");
                bean.luck_light_index = rs.getString("luck_light_index");

                bean.firstRunWeightConfig = rs.getString("firstRunWeightConfig");
                bean.luckResultWeightConfig = rs.getString("luckResultWeightConfig");
                bean.luckHitWeightConfig = rs.getString("luckHitWeightConfig");
                bean.rateConfig = rs.getString("rateConfig");

                bean.determiner_type = rs.getInt("determiner_type");
                bean.determiner_param = rs.getString("determiner_param");

                return bean;
            }
        } catch (SQLException sqlException) {
            sqlException.printStackTrace();
        } finally {
            IOUtils.close(rs);
        }

        GameResultBean lastBean = GameResultDB.findLast();
        return GameResultDB.findResult(runCount % lastBean.end_count);
    }


    public static GameResultBean findLast() {
        String sql = String.format("SELECT * FROM game_result_controller ORDER BY end_count DESC");

        ResultSet rs = DBHelper.executeQuery(sql);

        try {
            if (rs.next()) {
                GameResultBean bean = new GameResultBean();
                bean.type = rs.getInt("type");
                bean.start_count = rs.getInt("start_count");
                bean.end_count = rs.getInt("end_count");
                bean.first_run_light_index = rs.getString("first_run_light_index");
                bean.rate = rs.getInt("rate");
                bean.luck_type = rs.getInt("luck_type");
                bean.luck_light_index = rs.getString("luck_light_index");

                bean.firstRunWeightConfig = rs.getString("firstRunWeightConfig");
                bean.luckResultWeightConfig = rs.getString("luckResultWeightConfig");
                bean.luckHitWeightConfig = rs.getString("luckHitWeightConfig");
                bean.rateConfig = rs.getString("rateConfig");

                bean.determiner_type = rs.getInt("determiner_type");
                bean.determiner_param = rs.getString("determiner_param");

                return bean;
            }
        } catch (SQLException sqlException) {
            sqlException.printStackTrace();
        } finally {
            IOUtils.close(rs);
        }

        return null;
    }

}
